package com.dianrui.agent.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dianrui.agent.Constant;
import com.dianrui.agent.activity.BaseActivity;
import com.dianrui.agent.util.SPUtils;
import com.dianrui.agent.util.Static;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {

    public SPUtils spUtils;

    protected View rootView;

    protected Activity mContext;

    @Override
    public void onAttach(Activity activity) {
        if (activity instanceof BaseActivity) {
            mContext = activity;
        }
        super.onAttach(activity);

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getContentViewId(),container,false);
        ButterKnife.bind(this, rootView);//绑定framgent
        spUtils = new SPUtils(getActivity(), Constant.cfgFileName, Context.MODE_PRIVATE);
        initView();
        return rootView;
    }

    public abstract int getContentViewId();//布局ID

    public abstract void initView();

    public void JumpActivitys(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    protected void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(mContext, clazz);
        startActivity(intent);
        mContext.finish();
    }

    @Override
    public void setMenuVisibility(boolean menuVisibile) {
        super.setMenuVisibility(menuVisibile);
        if (this.getView() != null) {
            this.getView().setVisibility(menuVisibile ? View.VISIBLE : View.GONE);
        }
    }

    public Resources getSysResources() {//所有Fragment里面获取Resource都用这个
        if (isAdded()) {
            return getResources();
        } else {
            return Static.CONTEXT.getResources();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
